package com.cash.app.ui.main.fg.repay.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil3.load
import coil3.request.placeholder
import com.cash.app.R
import com.cash.app.constant.AppConstant.OS_OVERDUE
import com.cash.app.constant.AppConstant.OS_PENDING_REPAY
import com.cash.app.custom.CommonDialog
import com.cash.app.databinding.DialogAmountDetailsBinding
import com.cash.app.databinding.FgRepayItemBinding
import com.cash.app.manager.model.LoanOrderItem
import com.cash.app.utils.NumberUtils
import com.cash.app.utils.hide
import com.cash.app.utils.visible

class RepayListAdapter : RecyclerView.Adapter<RepayListAdapter.ViewHolder>() {

    private var items: List<LoanOrderItem> = emptyList()

    var onRepayClickListener: ((LoanOrderItem) -> Unit)? = null
    var onItemClickListener: ((LoanOrderItem) -> Unit)? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setData(newItems: List<LoanOrderItem>) {
        items = newItems
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = FgRepayItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount(): Int = items.size

    inner class ViewHolder(private val binding: FgRepayItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: LoanOrderItem) {
            with(binding) {

                if (item.p_icon.isNotEmpty()) {
                    productIcon.load(item.p_icon) {
                        placeholder(R.mipmap.app_icon)
                    }
                }
                productName.text = item.p_name

                if (item.order_status == OS_PENDING_REPAY || item.order_status == OS_OVERDUE) {
                    repayAmount.text = NumberUtils.toRupeeWithComma(item.r_current_money)
                } else {
                    repayAmount.text = NumberUtils.toRupeeWithComma(item.order_money)
                }

                orderNumber.text = item.o_id.toString()
                dueDate.text = item.due_time_string

                val isOverdue = item.order_status == OS_OVERDUE
                overdueTag.visibility = if (isOverdue) View.VISIBLE else View.GONE
                overdueTip.visibility = if (isOverdue) View.VISIBLE else View.GONE

                overdueTip.setOnClickListener {
                    showAmountDetailsDialog(item)
                }

                btnRepay.setOnClickListener {
                    onRepayClickListener?.invoke(item)
                }
                fgRepayItemCl.setOnClickListener {
                    onItemClickListener?.invoke(item)
                }
            }
        }

        private fun showAmountDetailsDialog(item: LoanOrderItem) {
            val context = binding.root.context
            val dialogView = DialogAmountDetailsBinding.inflate(LayoutInflater.from(context))

            // Set values
            with(dialogView) {
                lineOfCreditValue.text = NumberUtils.toRupeeWithComma(item.order_money)
                amountPaidValue.text = NumberUtils.toRupeeWithComma(item.r_already_money)
                val isOverdue = item.order_status == OS_OVERDUE
                if (isOverdue) {
                    overdueFeesLabel.visible()
                    overdueFeesValue.visible()
                    overdueFeesLabel.text =
                        context.getString(R.string.overdue_fees, item.r_late_day.toString())
                    overdueFeesValue.text = NumberUtils.toRupeeWithComma(item.r_late_fee)
                } else {
                    overdueFeesLabel.hide()
                    overdueFeesValue.hide()
                }
            }

            CommonDialog.Builder(context).apply {
                showTitle(true)
                setTitle(context.getString(R.string.amount_details))
                setContentView(dialogView.root)
                showButtons(true)
                addButton(context.getString(R.string.ok), { dialog ->
                    dialog.dismiss()
                })
                setCancelable(true)
                setCancelableOnTouchOutside(true)
            }.build().show()
        }
    }
} 